package com.cqupt.command.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cqupt.utils.FileReadMan;
import com.cqupt.utils.Result;
import com.cqupt.command.Command;

public class CatCommand implements Command{
	// 选项列表
		private List<String> options;
		// 参数列表..
		private String parameters;
		// 上命令的结果
		private String result;

		public CatCommand(String command, String result) {
			// 命令解析得到选项
			this.options = getOptions(command);
			// 命令解析得到参数
			this.parameters = getParameters(command);
			// 得到上个命令的结果
			this.result = result;
		}

		private List<String> getOptions(String command) {
			List<String> result = new ArrayList<>();
			Pattern pattern = Pattern.compile("-[a-z,A-Z]+");
			Matcher matcher = pattern.matcher(command);
			while (matcher.find()) {
				String parameterA = matcher.group();
				char[] chars = parameterA.substring(1, parameterA.length()).toCharArray();
				for (int i = 0; i < chars.length; i++) {
					result.add(chars[i] + "");
				}
			}
			return result;
		}

		private String getParameters(String command) {
			String[] cmd = command.split(" ");
			return cmd[cmd.length - 1].trim();
		}

		
		// 默认打开文件，   -b 对非空行输出行，    -n  对所有行显示行号 
		@Override
		public String run() {
			// 确定选项
			boolean onlyOpenFile = false;// 是否只打开文件
			boolean emptyLine = false;// 是否对非空行输出行  
			boolean displayNum = false;// false：默认不显示，true:显示
			for (int i = 0; i < options.size(); i++) {
				String option = options.get(i);
				switch (option) {
				case "b":
					emptyLine = true;
					break;
				case "n":
					displayNum = true;
					break;
				case "":
					onlyOpenFile = true;
					break;
				}
			}
			// 获取数据源
			List<String> listString = new ArrayList<>();
			List<Double> listDouble = new ArrayList<>();
			if (result != null) {
				String[] resultSource = result.split("\r?\n");
				if (displayNum) {
					for (int i = 0; i < resultSource.length; i++) {
						String sourceLine = resultSource[i];
						String tempLine = "";
						tempLine = tempLine + (i+1) +"\t"+sourceLine;
						listString.add(tempLine);
					}
				}else if (emptyLine){
					for (int i = 0; i < resultSource.length; i++) {
						String sourceLine = resultSource[i];
						int tempNum = 1;
						String tempLine = "";
						if (!sourceLine.trim().isEmpty()) {
							tempLine = tempLine + tempNum +"\t"+sourceLine;
							listString.add(tempLine);
							tempNum++;
						}
					}	
				}else {
					for (int i = 0; i < resultSource.length; i++) {
						String sourceLine = resultSource[i];
						listString.add(sourceLine);
					}
				}
			} else {
				FileReadMan fileReadMan = new FileReadMan(parameters);
				int tempNum = 1;
				if (displayNum) {
					while (fileReadMan.hasNext()) {
						String sourceLine = fileReadMan.next();
						String tempLine = "";
						tempLine = tempLine + tempNum +"\t"+sourceLine;
						listString.add(tempLine);
						tempNum++;
					}
				}else if (emptyLine){
					while (fileReadMan.hasNext()) {
						String sourceLine = fileReadMan.next();
						String tempLine = "";
						if (!sourceLine.trim().isEmpty()) {
							tempLine = tempLine + tempNum +"\t"+sourceLine;
							listString.add(tempLine);
							tempNum++;
						}
					}	
				}else {
					while (fileReadMan.hasNext()) {
						String sourceLine = fileReadMan.next();
						listString.add(sourceLine);
					}
				}
			}
			Result<String> result = new Result<String>();
			return result.getResult(listString);
		}			
}
